package jlu.zqq.dao;

import java.util.List;

import jlu.zqq.po.Fault;
import jlu.zqq.po.User;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for Fault
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see jlu.zqq.po.Fault
 * @author MyEclipse Persistence Tools
 */
public class FaultDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(FaultDAO.class);
	// property constants
	public static final String REASON_ID = "reasonId";
	public static final String FNAME = "fname";
	public static final String DESCRIBE = "describe";
	public static final String SYMPTOM = "symptom";
	public static final String ANALYSE = "analyse";
	public static final String MEASURE = "measure";
	public static final String SUBSYSTEM = "subsystem";
	public static final String SERVE = "serve";
	public static final String RESULTS = "results";

	public void save(Fault transientInstance) {
		log.debug("saving Fault instance");
		try {
			getSession().beginTransaction();
			getSession().save(transientInstance);
			getSession().getTransaction().commit() ;
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}finally{
			getSession().close();
		}
	}

	public void delete(Fault persistentInstance) {
		log.debug("deleting Fault instance");
		try {
			getSession().beginTransaction();
			getSession().delete(persistentInstance);
			getSession().getTransaction().commit() ;
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			getSession().close();
		}
	}
	public void deleteById(java.lang.Integer id){
		log.debug("deleting Fault By ID");
		try {
			String queryString="delete Fault as f where f.id="+id;
			getSession().beginTransaction();
			Query query=getSession().createQuery(queryString);
			getSession().getTransaction().commit();
			System.out.println();
		}catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			getSession().close();
		}
	}

	public Fault findById(java.lang.Integer id) {
		log.debug("getting Fault instance with id: " + id);
		try {
			Fault instance = (Fault) getSession().get("jlu.zqq.po.Fault", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List findByExample(Fault instance) {
		log.debug("finding Fault instance by example");
		try {
			List results = getSession().createCriteria("jlu.zqq.po.Fault")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Fault instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Fault as model where model."
					+ propertyName + " = '?'";
			System.out.println(queryString);
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByReasonId(Object reasonId) {
		return findByProperty(REASON_ID, reasonId);
	}

	public List findByFname(String fname) {
		log.debug("finding Fault instance with property: " 
				+ ", value: " );
		try {
			String queryString = "from Fault as model where model.fname = '"
					+ fname + " '";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByDescribe(Object describe) {
		return findByProperty(DESCRIBE, describe);
	}

	public List findBySymptom(String sname ,String symptom) {
		log.debug("finding Fault instance with property: " 
				+ ", value: " );
		try {
			String queryString ;
			if(sname!=null&&!sname.equals("")){
				queryString = "from Fault as model where model.subsystem='"+sname
					+"' and model.symptom like '%"+symptom+"%'";
			}else{
				queryString = "from Fault as model where model.symptom like '%"+symptom+"%'" ;
			}
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findBySymptomAndServe(String serve,String sname ,String symptom) {
		log.debug("finding Fault instance with property: " 
				+ ", value: " );
		try {
			String queryString ;
			if(symptom==null&&sname==null){
				queryString = "from Fault as model where model.serve='"+serve+"'";
			}else if(sname!=null&&!sname.equals("")){
				queryString = "from Fault as model where model.subsystem='"+sname
					+"' and model.serve='"+serve+"' and model.symptom like '%"+symptom+"%'";
			}else{
				queryString = "from Fault as model where model.serve='"+serve
						+"' and model.symptom like '%"+symptom+"%'" ;
			}
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByAnalyse(Object analyse) {
		return findByProperty(ANALYSE, analyse);
	}

	public List findByMeasure(Object measure) {
		return findByProperty(MEASURE, measure);
	}

	public List findBySubsystem(Object subsystem) {
		return findByProperty(SUBSYSTEM, subsystem);
	}

	public List findByServe(String serve) {
		log.debug("finding Fault instance with property: " 
				+ ", value: " );
		try {
			String queryString = "from Fault as model where model.serve='"
					+ serve+"'" ;
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByResults(Object results) {
		return findByProperty(RESULTS, results);
	}

	public List findAll() {
		log.debug("finding all Fault instances");
		try {
			String queryString = "from Fault";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public Fault updateFault(Fault fault) {
		log.debug("updating User instance");
		try {
			getSession().beginTransaction();
			getSession().update(getSession().merge(fault));
			
			getSession().getTransaction().commit() ;
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}finally {  
			getSession().close();  
        }  
		return fault;
	}
	public Fault merge(Fault detachedInstance) {
		log.debug("merging Fault instance");
		try {
			Fault result = (Fault) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Fault instance) {
		log.debug("attaching dirty Fault instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Fault instance) {
		log.debug("attaching clean Fault instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}